// Title of tool: Luts Macros and Tools Updater.txt 
// First version: 17/11/2006

// Description:

// The "Luts Macros and Tools Updater" tool set perfoms a live-updating/restoring of the following ImageJ macros repertories:
// - /macros
// - /macros/tools
// - /macros/toolsets
// - /luts
// For a routine use of installed macros and tools, a plugin shortcut can be installed using the MacroPluginShortcutsTool.txt

// Author : Gilles Carpentier
// Faculte des Sciences et Technologies,
// Universite Paris 12 Val de Marne, France.

var distantmacrosrep = "http://rsb.info.nih.gov/ij/macros/";
var distanttoolsrep = "http://rsb.info.nih.gov/ij/macros/tools/";
var distanttoolsetsrep = "http://rsb.info.nih.gov/ij/macros/toolsets/";

var urllist="http://image.bio.methods.free.fr/ij/ijupdatetest/ListOfMacros.txt";distantmacrolink = "";
var KindOfMacro=0,report=0,updates=0,nMacroinL=0,nToolinL=0,nToolsetsinL=0,TheReport="";
var onlinedoclink = "http://image.bio.methods.free.fr/ijmacrolutupdater.html";

var distantlutsrep = "http://image.bio.methods.free.fr/ij/luts/"; distantmacrolink = ""; nLutsinL=0;
var urllistoflut="http://image.bio.methods.free.fr/ij/ijupdatetest/ListOfLUTs.txt";distantmacrolink = "";
var demoimagename = "blobs.gif",non=0;

var xx = requires138b(); // check version at install time
function requires138b() {requires("1.38b"); return 0; }


macro "Stop Lut Scan Tool - Ccc" {non=1;}

// macro updater menu tool
var uCmds = newMenu("Macro, Tools and Toolsets Updater Menu Tool",newArray("Macro, Tools and Toolsets Updater","Get the list of available macros, tools and toolsets"));
macro "Macro, Tools and Toolsets Updater Menu Tool - CcccD5fD6fD7fD8fD9fC78bD17D19D2aD33D37D3bD42D4cD75D95DceDd5Dd9De6Df7Df9CddeDa3C36bD27D28D3aD57D58D59D66D76D77D86D87Da7Db8Dd6De8De9CeeeD00D01D02D04D06D07D08D09D0bD0dD0fD10D11D12D14D1bD1dD1fD20D21D22D2dD30D31D32D40D46D47D48D49D50D5bD60D70D71D72D74D7dD80D81D82D84D8dD90D91D92D94D9dDa0Da1Da2Da4Db0Db1DbbDc0Dc1Dc6Dc7Dc8Dc9Dd0Dd1Dd2De0De1De2DedDf0Df1Df2Df4DfbDfdDffC8beD3cD3dD4dD5aD6aD79D7aD7bD85D8bD9aDaaDc3Dc4Dd3Dd4CeeeD03D05D0aD0cD0eD13D15D1cD1eD23D2eD3eD4aD55D6cD73D7cD7eD83D8cD8eD93D9cD9eDb5DcaDdeDe3DeeDf3Df5DfcDfeC559D18D26D34D35D36D41D51D61DafDbfDcfDdaDdbDddDeaDf8CcddD2fD5cD6dD6eDabDb2Db4Dc2DefC99bD16D24D39D45D54D56D64D65Da5DacDb6DbcDcbDd7DecDfaCdefD67D8aC59dD29D2bD68D69D78D88D96D97D98D99Da6Da8Da9Db9De5De7CacdD1aD2cD38D4bD4eD5dD5eD6bD89D9bDb3DbaDc5Dd8De4Df6C348D25D43D44Db7DccDdcDebCcccD3fD4fDdf"{
	cmd = getArgument();
	if (cmd!="-" && cmd == "Macro, Tools and Toolsets Updater") {MacroToolsToolsetsUpdater ();}
	if (cmd!="-" && cmd == "Get the list of available macros, tools and toolsets") {GetTheListofAvailableMacros ();}
}

// luts updater menu tool
var lCmds = newMenu("LUTs Updater Menu Tool",newArray("LUTs Updater","Get the list of available LUTs"));
macro "LUTs Updater Menu Tool - Ce40D40D41D42D43D44D45D46D47D48D49D4aD4bD4cD4dD4eD4fCfffD80D81D82D83D84D85D86D87D88D89D8aD8bD8cD8dD8eD8fD90D91D92D93D94D95D96D97D98D99D9aD9bD9cD9dD9eD9fDa0Da1Da2Da3Da4Da5Da6Da7Da8Da9DaaDabDacDadDaeDafDb0Db1Db2Db3Db4Db5Db6Db7Db8Db9DbaDbbDbcDbdDbeDbfDc0Dc1Dc2Dc3Dc4Dc5Dc6Dc7Dc8Dc9DcaDcbDccDcdDceDcfDd0Dd1Dd2Dd3Dd4Dd5Dd6Dd7Dd8Dd9DdaDdbDdcDddDdeDdfDe0De1De2De3De4De5De6De7De8De9DeaDebDecDedDeeDefDf0Df1Df2Df3Df4Df5Df6Df7Df8Df9DfaDfbDfcDfdDfeDffCd13D30D31D32D33D34D35D36D37D38D39D3aD3bD3cD3dD3eD3fCf90D60D61D62D63D64D65D66D67D68D69D6aD6bD6cD6dD6eD6fC60dD00D01D02D03D04D05D06D07D08D09D0aD0bD0cD0dD0eD0fCf70D50D51D52D53D54D55D56D57D58D59D5aD5bD5cD5dD5eD5fCb07D20D21D22D23D24D25D26D27D28D29D2aD2bD2cD2dD2eD2fCfb0D70D71D72D73D74D75D76D77D78D79D7aD7bD7cD7dD7eD7fC90bD10D11D12D13D14D15D16D17D18D19D1aD1bD1cD1dD1eD1fT8d12U" {
	cmd = getArgument();
	if (cmd!="-" && cmd == "LUTs Updater") {LUTsUpdater ();}
	if (cmd!="-" && cmd == "Get the list of available LUTs") {GetthelistofavailableLUTs ();}
}

// last luts instaled menu tool
var luts = lastInstalledLutVisu();
var vCmds = newMenu("Last Installed Luts Menu Tool",luts);
macro "Last Installed Luts Menu Tool - C037T0b11LT7b0aiTbb10L"{
	cmd = getArgument();
	if (cmd!="-" && cmd !="No last luts installed list file found" && cmd != "Record this tool into the \"tools\" folder, to use this menu tool." && cmd != "List of new installed LUTs:") {
		image=checkForImage(cmd);
		if (nImages > 0) {selectImage(image);}
		run(cmd);
	}
}

macro "On Line Documentation Action Tool - C000Da9DadDb6DbdDc5Dc6Dd3Dd4Dd5Dd6DddC20fD11D12D13D14D15D16D17D18D19D1aD1bD1cD1dD1eD1fC0f3D42D43D44D45D46D47D48D49D4aD4bD4cD4dD4eD4fC0feD32D33D34D35D36D37D38D39D3aD3bD3cD3dD3eC444DdeDefC73fD01Cfd0D62D63D64D65D66D67D68D69D6aD6bD6cD6dD6eCeeeD90D95Da1Da4Db0Db2Db3De0De8Df0Df7DfaC222DeeC06fD22D23D24D25D26D27D28D29D2aD2bD2cD2dD2eC7f0D52D53D54D55D56D57D58D59D5aD5bD5cD5dD5eCcccDbfDf9Cf74D70Ca99D97C888Da8CfffD9bD9cC000Dc4Dd2C64fD10C0f4D41C0ffD31D3fCf40D71D72D73D74D75D76D77D78D79D7aD7bD7cD7dD7eD7fCa7fD00Cfd0D61D6fCfefD91C444Db5DdaDe9C06fD21D2fC7f0D51D5fCeddD96Cf88D82D83D84D85D8bD8cCaaaDccDeaDfdC999DcfCfffD9fDafDb1Db8De1C666Dc7C84fD02D03D04D05D06D07D08D09D0aD0bD0cD0dD0eC333Da6Da7DaaDb9DbcDd9DdcCdddD9aDa2Da3Dc8DdbDecCaaaDabDaeDc1De3De4De5De6Cf89D8dC222DcdDedDfeDffC4ffD30CfffDebDf1Df2Df3Df4Df5Df6DfcC48fD20C4f6D40CeeeD92D93D94Da0DcbDf8DfbCbbbDb4Dd8C9f4D50C555Dc2C73fD0fC333DbbDc3Dc9Dd1CdddDe7Cf89D81D86D8aD8fC111DbaDd7CdeeDa5CbbbDb7Dc0De2Cf99D87D88D89C777DacDcaCcbbD9eCfaaD80C999D98Cf89D8eC999D9dC766D99C555DceC888DbeCfd4D60C777DdfC666Dd0"{
	if (File.openUrlAsString(urllist) == "") exit("You need an internet access to run this function.");
	showMessageWithCancel  ("A notice is avaible on line. Open it with your default web browser?");
	run("URL...", "url=["+onlinedoclink +"]");
} 

// ---- Macro like function -----

function MacroToolsToolsetsUpdater () {
	distantmacrolink = urllist;
	if (File.openUrlAsString(distantmacrolink) == "") exit("You need an internet access to run this function.");
	Dialog.create("Macro & Tool Updater");
	Dialog.addMessage("The updater will look for new available macros, tools and toolsets.");
	Dialog.addMessage("\nChoose Updater Options:");
	Dialog.addCheckbox("- Check for macros, tools and toolsets updates (longer query):", 0);
	Dialog.addMessage("This option check for modifications, corrections or improvements \nfor the Macros, Tool and Toolsets already installed onto your \nImageJ version.");
	Dialog.addMessage("\n ");
	Dialog.addCheckbox("- Make an installation report.", 1);
	Dialog.show();
	updates= Dialog.getCheckbox();
	report= Dialog.getCheckbox();
	// To get the contents of the distant list of macros.
	showStatus("Internet link...");
	macrotextnih =File.openUrlAsString(distantmacrolink);
	showStatus("");
	// To obtain the separated lists of macros and tools from the distant file list.
	lines=split(macrotextnih,"\n");
	getMandTNumber (lines);
	lMacro = newArray(nMacroinL); lTool = newArray(nToolinL);  lToolsets = newArray(nToolsetsinL);
	lMacro = lPlace (lines,lMacro,"Macro "); lTool = lPlace (lines,lTool,"Tool "); lToolsets = lPlace (lines,lToolsets,"Toolset ");
	// To get the contents of local ImageJ (Macros, Tools, Toolsets) folders, into separated lists of macros, tools and toolsets.
	macrosfolder = "";alert=0;
	if (getDirectory("macros") == "") {
		// built a macros repertory
		macrobuilt= getDirectory("startup") ;
		MacroDir = macrobuilt +"macros/" +File.separator;
		File.makeDirectory(MacroDir);
		if (!File.exists(MacroDir)) exit ("Unable to create the \" macros \" directory, something wrong in the ImageJ folder.\nTry again after restarting Image.");
		macrosfolder = macrobuilt +"macros/"; alert =1;
	}
	if (macrosfolder == "") macrosfolder = getDirectory("macros");
	macrostoolslocation = macrosfolder + "tools/";
	if (File.isDirectory(macrostoolslocation) == 0) {
		toolsDir = macrostoolslocation+File.separator;
		File.makeDirectory(toolsDir);
		if (!File.exists(toolsDir)) exit("Unable to create the \" tools \" directory, something wrong in the ImageJ folder.\nTry again after restarting Image.");
	}
	macrostoolsetlocation = macrosfolder + "toolsets/";
	if (File.isDirectory(macrostoolsetlocation) == 0) {
		toolsetsDir = macrostoolsetlocation+File.separator;
		File.makeDirectory(toolsetsDir);
		if (!File.exists(toolsetsDir)) exit("Unable to create the \" toolsets \" directory, something wrong in the ImageJ folder\nTry again after restarting Image.");
	}
	listofmacro = getFileList(macrosfolder); listoftools = getFileList(macrostoolslocation);listoftoolsets = getFileList(macrostoolsetlocation);

	NbMacro= getMacroNumber (listofmacro); NbTools= getMacroNumber (listoftools); NbToolsets= getMacroNumber (listoftoolsets); NbToolsandMacros=(NbMacro + NbTools +NbToolsets);
	FMacros = newArray(NbMacro); FTools =newArray(NbTools); FToolsets =newArray(NbToolsets);
	FMacros = lPlace (listofmacro,FMacros,""); FTools = lPlace (listoftools,FTools,""); FToolsets =lPlace (listoftoolsets,FToolsets,"");
 
	// To get the new macros, tools and toolsets present in the list, not present into the local ImageJ .
	nbNewMacro= (nMacroinL - nbThings(lMacro,FMacros)); nbNewTools= (nToolinL - nbThings(lTool,FTools));nbNewToolsets= (nToolsetsinL - nbThings(lToolsets,FToolsets));
	NewMacros = newArray(nbNewMacro); NewTools =newArray(nbNewTools);NewToolsets =newArray(nbNewToolsets);
	NewMacros = PlaceNewThings (lMacro,FMacros,NewMacros); NewTools = PlaceNewThings (lTool,FTools,NewTools);NewToolsets = PlaceNewThings (lToolsets,FToolsets,NewToolsets);

	// To get the new macros and tool updates available for the local ImageJ macros and tools.
	if(updates==1) {
		SortedNewMacros = newArray(NbMacro);
		UpdatedMacro = newArray(Nbmacroupdates (FMacros,lMacro,distantmacrosrep,macrosfolder,SortedNewMacros));
		UpdatedMacro = placeUpdatemacro(UpdatedMacro,FMacros,SortedNewMacros);
		SortedNewTools = newArray(NbTools);
		UpdatedTool = newArray(Nbmacroupdates (FTools,lTool,distanttoolsrep,macrostoolslocation,SortedNewTools));
		UpdatedTool = placeUpdatemacro(UpdatedTool,FTools,SortedNewTools);
		SortedNewToolsets = newArray(NbToolsets);
		UpdatedToolsets = newArray(Nbmacroupdates (FToolsets,lToolsets,distanttoolsetsrep,macrostoolsetlocation,SortedNewToolsets));
		UpdatedToolsets = placeUpdatemacro(UpdatedToolsets,FToolsets,SortedNewToolsets);
 
	} else {UpdatedMacro=0;UpdatedTool=0;UpdatedToolsets=0;}
	// To make a dialog box to the user.
	Messagenew=""; messageupdat="";
	message1="- No Macro, Tool or Toolset update available.";
	message2="- There is no new Macro, Tool or Toolset available.";
	message3="- Macros, Tools and Toolsets Updates were not queried.";
	if ((NewMacros.length + NewTools.length +NewToolsets.length) > 0) {
				Messagenew = "- There are "+ NewMacros.length + " new Macro(s), " + NewTools.length + " new Tool(s) and " + NewToolsets.length + " new Toolset(s), available on line.";
	} else {Messagenew = message2;}

	if(updates==1) {
		if ((UpdatedMacro.length + UpdatedTool.length + UpdatedToolsets.length) > 0) {
			messageupdat="- There are "+ UpdatedMacro.length + " Macro(s), "+ UpdatedTool.length +" Tool(s) and "+ UpdatedToolsets.length +" Toolset(s), updates available on line.";
		} else {messageupdat= message1;}
	} else {messageupdat = message3;} 
	continue = "";
	if ((messageupdat != message3) ||  (Messagenew != message2)) continue = " \n-> Perform the installation?";
	if ((Messagenew== message2) && (messageupdat== message1)) exit("No new macro, tool or toolset update available: your ImageJ Macro folder is up to date.");
	showProgress(1.0);
	showMessageWithCancel("Installation User Confirmation",Messagenew +"\n"+ messageupdat + "\n"+ continue);
	if (continue == "") exit;
	// To perform the installation of the new macros and tools.
	if ((NewMacros.length + NewTools.length + NewToolsets.length) > 0) {
		instalmacro(NewMacros,distantmacrosrep,macrosfolder);
		instalmacro(NewTools,distanttoolsrep,macrostoolslocation);
		instalmacro(NewToolsets,distanttoolsetsrep,macrostoolsetlocation);
	}
	// To update the macros and tools.
	if (updates==1) {
		if ((UpdatedMacro.length + UpdatedTool.length + UpdatedToolsets.length) > 0) {
			archmacros(UpdatedMacro,macrosfolder,"Archived Macros");
			archmacros(UpdatedTool,macrostoolslocation,"Archived Tools");
			archmacros(UpdatedToolsets,macrostoolsetlocation,"Archived Toolsets");
			instalmacro(UpdatedMacro,distantmacrosrep,macrosfolder);
			instalmacro(UpdatedTool,distanttoolsrep,macrostoolslocation);
			instalmacro(UpdatedToolsets,distanttoolsetsrep,macrostoolsetlocation);
		}
	}
	// To make the installation report.
	if (report==1) rapport (FMacros,FTools,FToolsets,lMacro,lTool,lToolsets,NewMacros,NewTools,NewToolsets,UpdatedMacro,UpdatedTool,UpdatedToolsets,macrosfolder,"Update reports");
	if (alert==1) showMessage ("Restart ImageJ to finish the installation of the macros resources.");
}
	// Contents of arrays:
	// FMacros, FTools ; lists of macros and tools into your ImageJ.
	// lMacro, lTool, lToolsets ; lists of macros, tools and toolsets into the online ImageJ macro list.
	// NewMacros, NewTools, NewToolsets ; lists of macros, tools and toolsets into the online ImageJ macro list.
	// UpdatedMacro, UpdatedTool, UpdatedToolsets ; lists of macros , tools and toolsets updates available on the ImageJ web site.


function GetTheListofAvailableMacros () {
	showMessageWithCancel ("Get on line available macros list?");
	if (File.openUrlAsString(urllist) == "") exit("You need an internet access to run this function.");
	run("URL...", "url=["+urllist+"]");
}

// lut part

function LUTsUpdater () {
	distantmacrolink = urllistoflut;
	message="The updater allows to visualize the LUTs on a\"ramp\",\nor their effects onto an image.";
	Dialog.create("Lut Updater User Options");
	Dialog.addMessage(message);
	Dialog.addChoice("->",newArray("Ramp","Image","No Visualize"));
	Dialog.addCheckbox("Update report?", 1);
	Dialog.show();
	visuchoice=Dialog.getChoice();
	lutreport= Dialog.getCheckbox();

	if (File.openUrlAsString(distantmacrolink) == "") exit ("You need an internet access to run this function.");
	showStatus("Internet link...");
	luttextnih =File.openUrlAsString(distantmacrolink);
	showStatus("");
	lines=split(luttextnih,"\n");nLuts=GetnLUTs (lines);
	lLuts = newArray(nLuts);lLuts=llutPlace (lines,lLuts);
	if (getDirectory("startup") == 0) exit ("Unable to find the startup directory, something wrong in the ImageJ folder.\nTry again after restarting Image.");
	ijpath = getDirectory("startup");lutpath=ijpath+"luts/";
	if (File.isDirectory(lutpath) == 0) {
		LutDir = lutpath +File.separator;
		File.makeDirectory(LutDir);
		if (!File.exists(LutDir)) exit ("Unable to create the \" luts \" directory, something wrong in the ImageJ folder.\nTry again after restarting Image.");
	}
	listoflut = getFileList(lutpath);nlocallut=GetnLUTs (listoflut);
	llocalLuts = newArray(nlocallut);llocalLuts=llutPlace (listoflut,llocalLuts);
	nbNewLut= (nLuts - nbThings(lLuts,llocalLuts));lNewlut=newArray(nbNewLut);
	lNewlut=PlaceNewThings (lLuts,llocalLuts,lNewlut);
	if (lNewlut.length >0) {
		message="Number of new available LUT(s): " + lNewlut.length + ". Perform the installation? ";
		if (visuchoice != "No Visualize") message = message+ "\n(The scan of new installed LUTs can be interrupted by clicking \nonto the visualisation image window).";
		showMessageWithCancel (message);
	}
	instalmacro(lNewlut,distantlutsrep,lutpath);

	if (lNewlut.length >0) {
		if (lutreport == 1 ) { lutRapport (lutpath,lNewlut);}
		if (visuchoice != "No Visualize") {
			if (visuchoice == "Image") OpenImageLink(0);
			if (visuchoice == "Ramp") makeRamp();
		}
		if (visuchoice != "No Visualize") {
			if (lNewlut.length>0) {
				show=1;
				showNewLuts(lutpath,lNewlut,show);
				message="Result of the LUT update process: "+lNewlut.length+" new LUTs has been";
				message=message+"\ninstalled. Use the \"Last Installed Luts Menu Tool\" to see";
				message=message+"\nagain a particular LUT.";
				message=message+"\n \n(Restart ImageJ to make the LUTs available from the";
				message=message+"\n\Image -> Lookup Tables\" menu).";
				showMessage (message);
				UpdateLutList();
			}
		} 
	} else { showMessage ("No new lut available. Your \"luts\" folder is up to date.");}
}

function lastInstalledLutVisu() {
	lutlistfile = getDirectory("startup")+"luts"+File.separator+ " LUTs Update Reports"+File.separator+"Last New Installed Luts.txt";
	if (!File.exists(lutlistfile)) return newArray ("No last luts installed list file found");	list = newArray("No last luts installed list file found");listOfCommands = newArray("no commands");
	lastlutlist =File.openAsString(lutlistfile);
	lines=split(lastlutlist,"\n");
	listOfCommands=newArray(lines.length+2);
	listOfCommands[0]="List of new installed LUTs:";listOfCommands[1]="-";
	for (i=0; i<lines.length; i++) {listOfCommands[i+2]=lines[i];}
	return listOfCommands;
}
function GetthelistofavailableLUTs () {
	if (File.openUrlAsString(urllistoflut) == "") exit("You need an internet access to run this function.");
	showMessageWithCancel ("Get on line available LUTs list?");
	run("URL...", "url=["+urllistoflut +"]");
}

function UpdateLutList() {
	kind="tools";macroname="Luts Macros and Tools Updater.txt";
	macropath=getDirectory("macros") + kind + File.separator+ macroname;
	if (!File.exists(macropath)) exit ("Record this tool into the \"tools\" folder with the name \"" +macroname+ "\",\nto use this menu tool.");
	run("Install...", "install=["+macropath+"]");
}

macro "About Luts Macros and Tools Updater Action Tool - C000D84Cb9fD25De7CaaaD14D2dDa0DafDecDfaCedfD49D4aD4bD4cD58D68D9bDb9DbaDbbDbcC889D2cDebCddfD52CcccD0bD22CeeeD00D03D0cD0fD10D1fD20D2fD30D40Dc0Dd0DdfDe0DefDf0Df1Df2Df3DfcDfeDffC666D07D70CdcfD34D35Dc4CbacD86D91CfefD6bD6dD7cD8cD8dD8eD9cD9dDadC97aDd3De5CedfD99CeeeD01D02D04D0dD0eD11D12D1eD21D3fDcfDd1De1De2DeeDf4DfdCfefD7dC545D94Da5CdbeDa4Da7CbabD05D50DaeCfefD7eC98aD32Da1CecfD39D3aD3bD46D48D57D67Da8Db6Db8Dc9DcaDcbDccCdcdD81C878D1bD60D65CdcfD29D36D38D47D77Db7Dc8Dd9DdaCcbcD7aDbfDc1De3C98bD16D24D75DeaCedfD56D66D73D76D83D93Da3C212D7bD88D96D97CcaeD26D3cDdbCaaaD3eD5fCfdfD59C889D15D1aD78Dc2CdcfD45Db4Db5Dc6CdddD13D31D4fDdeDedDfbC777D09D7fD85D90Df7CeceDbdCbadD18D55Db2De9Ca9aD5eDcdDceDdcC656D08D64D80D87D8bCdbfD28D2aD37Dc7Dd8CbbbD1cD42Dd2Df5CfdfD5aD5bD5cD5dD69D6aD6cD9aDa9DabDacC999D0aD41DddDf6CdddD1dD2eD9eDb0C888D06D4eD6fD9fDf9CcbdD54D71D98Dc3Ca9dD17D19Dd4De6C000D74D79D95CcafDd5Dd6De8CedfD62D72D92C889D51Db1DbeCedfD53D63Da2CdcdD6eC777D8fDf8CdcfD43D44Db3Dc5CbadD2bD33C99aD23De4C545D89Da6CcbfD27Dd7CbabD61CedfD82DaaC98aD3dCdceD4dD8a" {
	about1();
}

// ---- functions ----- //
// Function giving the number (NumberOfMacro) of macros and tools contained a list (lmacros).
function getMacroNumber (lmacros) {
	NumberOfMacro=0;
     	for (i=0; i<lmacros.length; i++) {
         	showProgress(i,lmacros.length);
		if (endsWith(lmacros[i], ".txt") || endsWith(lmacros[i], ".ijm")) NumberOfMacro ++;	}
	return NumberOfMacro;
}

// Function giving the macros number (nMacroinL) and the tools number (nToolinL) containened in an array (lmacros).
// macros are sorted according .txt or .img and 'Macro' or 'Tool' prefixes.
function getMandTNumber (lmacros) {
	nMacroinL=0;nToolinL=0;nToolsetsinL=0;
     	for (i=0; i<lmacros.length; i++) {
         	showProgress(i,lmacros.length);
		if (endsWith(lmacros[i], ".txt") || endsWith(lmacros[i], ".ijm")) {
			if (startsWith(lmacros[i], "Macro ")) nMacroinL ++;
			if (startsWith(lmacros[i], "Tool ")) nToolinL ++;
			if (startsWith(lmacros[i], "Toolset ")) nToolsetsinL ++;
		}		
	}
}

// Function giving the list of macros (Lele) with the prefix (kind) contained in the list (lmacros).
function lPlace (lmacros,Lele,kind) {
	compteur=0;
     	for (i=0; i<lmacros.length; i++) {
         	showProgress(i,lmacros.length);
		if (endsWith(lmacros[i], ".txt") || endsWith(lmacros[i], ".ijm")) {
			if (startsWith(lmacros[i], kind)) {
				Lele[compteur]=substring(lmacros[i],lengthOf(kind),lengthOf(lmacros[i]));
				compteur ++;
			}
		}		
	}
	return Lele;
}

// Function setting in the report  (TheReport) the contents of the list (tab), adding the prefix (prefix).
function listit (tab,prefix) {
	for (i=0; i<tab.length; i++) {
		TheReport=TheReport + "\n"+ prefix+tab[i];
	}
}

// Function giving the number of elements contained in the first list (liste1) and in the second list (liste2).
function nbThings(liste1,liste2) {
	compteur=0;
     	for (i=0; i<liste1.length; i++) {
		showProgress(i,liste1.length);
		for (j=0; j<liste2.length; j++) {
			if (liste1[i]==liste2[j]) compteur ++;
		}
	}
	return compteur;
}

// Function giving the list of elements (New) contained in the first list (liste1) and in the second list (liste2).
function PlaceNewThings (liste1,liste2,New) {
	newcompteur=0;
	test=0;
	for (i=0; i<liste1.length; i++) {
		showProgress(i,liste1.length);
		for (j=0; j<liste2.length; j++) {
			if (liste1[i]==liste2[j]) test ++;
		}
		if (test==0) {
			New[newcompteur]=liste1[i];
			newcompteur ++;
		}
		test=0;
	}
	return New;
}

// Function sorting the number of elements (compteur) which are macros and tools, and can be updated (sorted [n], if n=1 the macro is updatable).
function Nbmacroupdates (macrolocalist,macrodistlist,urlrep,localrep,sorted) {
	compteur=0;
	for (i=0; i<macrolocalist.length; i++) {
		showProgress(i,macrolocalist.length);
		for (j=0; j<macrodistlist.length; j++) {
			if (macrolocalist[i]==macrodistlist[j]) {
				macrotextnih=getdistantmacro (macrodistlist[j],urlrep);				macropath = localrep + macrolocalist[i];
				if (File.exists(macropath)) {
					thelocalmacro = File.openAsString(macropath);
					if (thelocalmacro != macrotextnih) {
						if (macrolocalist[i] != "StartupMacros.txt") {
							compteur=compteur+1;
							sorted[i]=1;
						}
					}
				}
			}
		}
	}
	return compteur;
}

// Function giving the list (listupdate) of macros and tools which can be updated.
function placeUpdatemacro(listupdate,locallist,Sorted) {
	compteur=0;
	for (i=0; i<Sorted.length; i++) {
		if (Sorted[i] == 1) {
			listupdate[compteur]=locallist[i];
			compteur++;
		}
	}
	return listupdate;
}

// Function archiving the old macros.
function archmacros(list,localrep,foldername){
	// Create an Archived Tools directory in tools repertory
	if (list.length > 0) {
		ArchDir = localrep+ " " + foldername+File.separator;
		File.makeDirectory(ArchDir);
		if (!File.exists(ArchDir)) exit("Unable to create the "+foldername+" directory, something wrong in the ImageJ folder");
	}
	for (i=0; i<list.length; i++) {
		if (File.exists(localrep+list[i])) {
			thelocalmacro = File.openAsString(localrep+list[i]);
			f= File.open(ArchDir + list[i]);
			print (f,thelocalmacro);
			File.close(f);
		}
	}
}

// Function installing a list (list) of distant macro located at (urlrep), into the local folder (localrep).
function instalmacro(list,urlrep,localrep){
	for (i=0; i<list.length; i++) {
		showProgress(i,list.length);
		macrotextnih= getdistantmacro (list[i],urlrep);
		f= File.open(localrep + list[i]);
		print (f,macrotextnih);
		File.close(f);
	}
	showProgress(1.0);
}

// Function giving the content of a distant macro (name) located at the distant repertory (urlrep).
function getdistantmacro (name,urlrep) {
	distantmacrolink = urlrep + name;
	if (indexOf(distantmacrolink, " ") > -1) {
		while (indexOf(distantmacrolink, " ") > -1) {
			distantmacrolink=substring(distantmacrolink, 0, (indexOf(distantmacrolink, " ")))+"%20"+substring(distantmacrolink, (indexOf(distantmacrolink, " ")+1),lengthOf(distantmacrolink) );
		}
	}
	showStatus("Internet link...");
	macrotextnih =File.openUrlAsString(distantmacrolink);
	showStatus("");
	return macrotextnih;
}

// Function making a detailed report of every list obtained.
function rapport (FMacros,FTools,FToolsets,lMacro,lTool,lToolsets,NewMacros,NewTools,NewToolsets,UpdatedMacro,UpdatedTool,UpdatedToolsets,localrep,foldername) {
	TheReport="";
	getDateAndTime(year, month, dayOfWeek, dayOfMonth, hour, minute, second, msec);
	reportname=  ""+dayOfMonth+"-"+(month+1) +"-"+year+" at "+hour+"."+minute+"."+second+"";
	TheReport=TheReport + "Installation report for the update performed on: "+reportname;
	if ((NewMacros.length+NewTools.length +NewToolsets.length)>0) {
		TheReport=TheReport +"\n\n--- The following new Macros, Tools and Toolsets have been installed: ";
		TheReport=TheReport + "("+(NewMacros.length+NewTools.length+NewToolsets.length)+" Macros: "+NewMacros.length+ " \"macros\" , "+NewTools.length+" \"tools\" & "+NewToolsets.length+" \"toolsets\" ):";
		listit (NewMacros,"Macro ");
		if (NewTools.length > 0) TheReport=TheReport + "\n--------";
		listit (NewTools,"Tool ");
		if (NewToolsets.length > 0) TheReport=TheReport + "\n--------";
		listit (NewToolsets,"Toolset ");
	}
	if(updates==1) {
		TheReport=TheReport + "\n\n--- The following Macros, Tools and Toolsets update have been performed: ";
		TheReport=TheReport +"("+(UpdatedMacro.length+UpdatedTool.length+UpdatedToolsets.length)+" Macros: "+UpdatedMacro.length+ " \"macros\" , "+UpdatedTool.length+" \"tools\" & "+UpdatedToolsets.length+" \"toolsets\"):";
		listit  (UpdatedMacro,"Macro ");
		if (UpdatedTool.length > 0) TheReport=TheReport + "\n--------"; listit (UpdatedTool,"Tool ");
		if (UpdatedToolsets.length > 0) TheReport=TheReport + "\n--------"; listit (UpdatedToolsets,"Toolset ");
	}
	if (updates==0) TheReport=TheReport + "\n\n--- The Macros, Tools and Toolsets updated have not been queried.";
	TheReport=TheReport + "\n\n--- Note: the list of macros, tools and toolsets installed onto you computer before this update ---";
	TheReport=TheReport + "\n      ("+(FMacros.length+FTools.length+ FToolsets.length)+" Macros: "+ FMacros.length + " \"macros\" , "+FTools.length+" \"tools\" & "+ FToolsets.length+" \"toolsets\"):";
	listit (FMacros,"Macro ");TheReport=TheReport +"\n--------";listit (FTools,"Tool ");TheReport=TheReport +"\n--------";listit (FToolsets,"Toolset ");

	TheReport=TheReport +"\n\n--- Note: the list of macros, tools and toolsets available at the ImageJ web site ---";
	TheReport=TheReport + "\n      ("+(lMacro.length+lTool.length+ lToolsets.length)+" Macros: "+ lMacro.length + " \"macros\" , "+lTool.length+" \"tools\" & "+ lToolsets.length +" \"toolsets\"):";
	listit (lMacro,"Macro "); TheReport=TheReport +"\n--------"; listit (lTool,"Tool ");TheReport=TheReport +"\n--------"; listit (lToolsets,"Toolset ");
	UpdateReports = localrep+ " " + foldername+File.separator;
	File.makeDirectory(UpdateReports);
	if (!File.exists(UpdateReports)) exit("Unable to create the "+foldername+" directory, something wrong in the ImageJ folder");
	f= File.open(UpdateReports + reportname+".txt" );	print (f,TheReport);
	File.close(f);
	if (File.exists(UpdateReports + reportname+".txt")) open(UpdateReports + reportname+".txt");
}

function about1() {
	about="";
	about=about + "The \"Luts Macro and Tools Updater\" ImageJ tool bar icons allow to perfom a live";
	about=about + "\nupdate of the ImageJ macros and luts repertories.";
	about=about + "\n \n\"Macro, Tools and Toolsets Updater Menu Tool\" ImageJ tool bar icon:";
	about=about + "\n    - \"Macro, Tools and Toolsets Updater\" looks for new and updated macros,";
 	about=about + "\n       Tools and Toolsets and installs them.";
	about=about + "\n \n\"LUTs Updater Menu Tool\" ImageJ tool bar icon:";
	about=about + "\n    - \"LUTs Updater\" looks for new luts and installs them.";
	about=about + "\n \n\"Last Installed Luts Menu Tool\" ImageJ tool bar icon:";
	about=about + "\n    - Gives a menu of last new installed LUTs, allowing to test them on images or";
	 about=about + "\n      ramps.";
	about=about + "\n \n- Click on the \"On Line Documentation\" ImageJ tool bar icon for more details.";
	about=about + "\n- Click on the \"Version Infos\" ImageJ tool bar icon to look for new beta versions.";
	about=about + "\n------------------------------------------------------------------";
	about=about +"\nAuthor : Gilles Carpentier"+"\nFaculte des Sciences et Technologies"+"\nUniversite Paris 12 Val de Marne, France.";
	showMessage(about);
}
// function specific to the LUTs Updater

function OpenImageLink(question) {
	demoimalocation = getDirectory("startup");	
	fildestination = demoimalocation+ "Downloaded Demo Images/" +demoimagename;
	if (File.exists(fildestination) && ! isOpen(demoimagename)) {
		if (question ==1 ) showMessageWithCancel ("The \"" + demoimagename + "\" has already been downloaded. Open it?");
		open(fildestination);
	}
	else {
		if (! isOpen(demoimagename)) { run("Blobs (25K)"); }
		selectImage(demoimagename);
		nomdimage = getTitle;
		// Create a <Downloaded Demo Images> repertory in ImageJ folder.
		ImaDemo = demoimalocation+"Downloaded Demo Images"+File.separator;
		File.makeDirectory(ImaDemo);
		if (!File.exists(ImaDemo)) exit("Unable to create directory, something wrong in the ImageJ folder");
		selectWindow(nomdimage);
		save(""+ImaDemo+""+ nomdimage +"");
	}
}

function lutRapport (cheminlut,liste) {
	lutrapport="";LastLuts="";
	getDateAndTime(year, month, dayOfWeek, dayOfMonth, hour, minute, second, msec);
	reportname=  ""+dayOfMonth+"-"+(month+1) +"-"+year+" at "+hour+"."+minute+"."+second+"";
	lutrapport=lutrapport + "Installation report for the LUT update performed on: "+reportname;
	lutrapport=lutrapport +"\n \nThe following "+liste.length+ " new LUTs has been installed into the \""+cheminlut+ "\"\nrepertory of ImageJ:\n";	for (i=0; i<liste.length; i++) {
		lutrapport= lutrapport + "\nLut name: " + liste[i] ;
		LastLuts=LastLuts  + ShortName (liste[i]) + "\n";
	}
	foldername="LUTs Update Reports";
	LutUpdateReports = cheminlut+ " " + foldername+File.separator;
	File.makeDirectory(LutUpdateReports);
	if (!File.exists(LutUpdateReports)) exit("Unable to create the "+foldername+" directory, something wrong in the ImageJ folder");
	f= File.open(LutUpdateReports + reportname+".txt" );	print (f,lutrapport);
	File.close(f);
	f= File.open(LutUpdateReports + "Last New Installed Luts.txt" );	print (f,LastLuts);
	File.close(f);
	if (File.exists(LutUpdateReports + reportname+".txt")) open(LutUpdateReports + reportname+".txt");
}

// Function giving the number of LUTs in a list
function GetnLUTs (lutlist) {
	NumberOfLUTs=0;
     	for (i=0; i<lutlist.length; i++) {
         	showProgress(i,lutlist.length);
		if (endsWith(lutlist[i], ".lut")) NumberOfLUTs ++;	}
	showProgress(1.0); return NumberOfLUTs;
}

// Function giving the list of elements (Lele) with the sufix ".lut" contained in the list (llut).
function llutPlace (llut,Lele) {
	compteur=0;
     	for (i=0; i<llut.length; i++) {
         	showProgress(i,llut.length);
		if (endsWith(llut[i], ".lut")) {Lele[compteur]=llut[i];compteur ++;}	}
	showProgress(1.0); return Lele;
}

// function opening the new luts
function showNewLuts(cheminLut,lNewlut,show) {
	nomderampe = getTitle; setTool(10);
     	for (i=0; i<lNewlut.length; i++) {
		if (show ==1) {
			open(cheminLut + lNewlut[i]);
			rename(lNewlut[i]);
			if (non == 0) wait(500);
		}
	}
	rename (nomderampe);non=0;
}

function makeRamp () {
	setBatchMode(true);
	if (! isOpen("LUTs visualisation")) { 
		newImage("LUTs visualisation", "8-bit ramp", 256, 32,1);
	} else {selectImage("LUTs visualisation");}
	nomderampe = getTitle;
	setBatchMode("exit and display");
	return nomderampe;
}

function checkForImage (cmd) {
	imaname="";
	if (nImages > 0) {
		setBatchMode(true);visu=0;depth = bitDepth;imaname= getTitle;ok=0;
		if (depth > 8) {
			showMessage("Color tables cannot be assigned to RGB image.\nThe lut will be drawed on a \"ramp\" window,\nor applied to the demo image.");
			rampname= lookForaRamp ();
			if  (rampname == "") {makeRamp ();selectImage("LUTs visualisation");imaname= getTitle;depth = bitDepth;ok=1;}
			if  (rampname != "" && ok==0) {selectImage(rampname);imaname= getTitle;depth = bitDepth;ok=1;}
		} 
		if (depth == 8 && startsWith(imaname, "LUTs :" ) && ok==0) ok=1;
          	if (depth == 8 && imaname == demoimagename)  ok=1;
          	if (depth == 8 && imaname == "LUTs visualisation")  ok=1;
		selectImage (imaname);
		if (ok ==1 && depth ==8) {rename ("LUTs : " + cmd);}
		imaname= getTitle;
		setBatchMode("exit and display");
	}
	return imaname;	
}

function lookForaRamp () {
	rampansvers = "";
	if (nImages > 0) {
		for (i=1; i <= nImages; i++) {
				selectImage(i);imaname=getTitle;
				if (bitDepth == 8 && startsWith(imaname, "LUTs :")) {rampansvers=imaname;}
			} 
		}
	return rampansvers;
}

function ShortName (longname) {
	if (endsWith(longname, ".txt") || endsWith(longname, ".ijm") || endsWith(longname, ".lut")) longname = substring(longname, 0, (lengthOf(longname)-4));
	return longname;
}


// --- End of code of the macro project ---//
// ----------------------------------//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
// -------------------*** Additionnal code for on line update resources ***-----------------------------

//Developer info
//Kind:Tool
//Title:"Luts Macros and Tools Updater" 
//Version:1.0
//Date:17 November 2006
//Origin:NIH
//End info

macro "Version Infos Action Tool - C0589T1e16VTcf13?" {

	// variables for on line update resources
	beginsign="//Developer info";endsign="//End info"; 
	kind="tools/"; 
	urlrep="http://image.bio.methods.free.fr/ij/ijmacro/macroupdater/";
	name="Luts Macros and Tools Updater.txt"; 
	namedev="Luts Macros and Tools Updater-dev.txt"; 
	favoritefoldername= "Image.Bio.Methods";

	version=versionMessage();
	if (indexOf(version, "install it?" ) > 0 ) {
		macrotext=getdistantmacro (namedev,urlrep);macrolocal="";
		macropath=getDirectory("macros")+kind+namedev;
		if (File.exists(macropath)) {macrolocal=File.openAsString(macropath);}
		if (macrotext != macrolocal) {
			//perfom the installation
			Dialog.create("New version installation option");
			Dialog.addMessage(version);
			Dialog.addCheckbox("Install a Plugin Shortcut?", 0);
			Dialog.addMessage("(This option provides a shortcut in the plugins menu of ImageJ, making easier\nthe next use of the new installed version).");
			Dialog.show();
			plugin= Dialog.getCheckbox();
			f= File.open(macropath);
			print (f,macrotext);
			File.close(f);
			if (plugin ==1) {InstallPluginsStarter(namedev);}
			message="The installation of the "+giveDevInfo (macrotext,1)+ " "+ giveDevInfo (macrotext,2)+ "is completed.";
			message=message+ " Do you want to run it?";
			showMessageWithCancel(message);
			run("Install...", "install=["+macropath+"]");
		}
	} else {showMessage (version); // comment without installation available}
}

function versionMessage() {
	
	version="";
	if (getDirectory("startup") == 0) exit ("Unable to find the startup directory, something wrong in the ImageJ folder");
	if (getDirectory("macros") == 0) exit ("Unable to find the macros directory, something wrong in the ImageJ folder");
	MacroPath=getDirectory("macros");thismacropath=MacroPath+kind+name;
	if (! File.exists(thismacropath)) exit ("This macro has to be recorded under the name of \"" +name+"\"\ninto the \"macros/"+kind+"\" folder of ImageJ.");

	macrotext=File.openAsString(thismacropath);
	macrotextdistant=getdistantmacro (namedev,urlrep);

	version="";macrolocal="";
	version=version + "\n \nThis version of the " + giveDevInfo (macrotext,1) + " " + giveDevInfo (macrotext,2);
	version=version + "is provided by the " + giveDevInfo (macrotext,5)+ " web site.";
	version=version + "\nVersion number: " + giveDevInfo (macrotext,3)+ " - " + giveDevInfo (macrotext,4) +".";
	
	if (macrotextdistant !="" ) {
		new=giveDevInfo (macrotextdistant,3);old=giveDevInfo (macrotext,3);
		if (new > old) {
			macropath=getDirectory("macros")+kind+namedev;
			if (File.exists(macropath)) {macrolocal=File.openAsString(macropath);}
			if (macrotextdistant != macrolocal) {
				update="\n \nA new beta version "+new+ " is available on the "  +giveDevInfo (macrotextdistant,5)+ " web site: ";
				update=update+ "\n \nDo you want to install it?";
			} else {
				update ="\n \nThe new "+new+" beta version called \"" +namedev+ "\" provided by \nthe "+giveDevInfo (macrotextdistant,5) +" web site has already be installed";
				update = update+ " in the \"" +giveDevInfo (macrotext,1)+ "\" repertory \nof ImageJ.";
			}
		} else {
			update="No new Beta version available.";
		}
		version=version +"\n" + update ;
	} 
	return version; 
}

function giveDevInfo (text,n) {
	lines=split(text,"\n");
	if ( (indexOf(text, beginsign)<0) || (indexOf(text, endsign)<0) ) exit ("Not upgradable macro code.");
	for (i=0; lines[i] != endsign; i ++) {}
	for (j=i; lines[j] != beginsign; j --) {}
	infotext=newArray(i-j-1);
	for (i=0; i < infotext.length; i ++) {infotext[i]=lines[i+j+1];}
	info=infotext[n-1]; signature=":";
	cut = indexOf(info, signature);
	info = substring(info,(cut+lengthOf(signature)),lengthOf(info));
	return info;
}

// Function giving the content of a distant macro (name) located at the distant repertory (urlrep).
function getdistantmacro (name,urlrep) {
	macrotextnih="";
	if (File.openUrlAsString("http://rsb.info.nih.gov/ij/macros/Arrays.txt") != "") {
		distantmacrolink = urlrep + name;
		if (indexOf(distantmacrolink, " ") > -1) {
			while (indexOf(distantmacrolink, " ") > -1) {
				distantmacrolink=substring(distantmacrolink, 0, (indexOf(distantmacrolink, " ")))+"%20"+substring(distantmacrolink, (indexOf(distantmacrolink, " ")+1),lengthOf(distantmacrolink) );
			}
		}
		showStatus("Internet link...");
		macrotextnih =File.openUrlAsString(distantmacrolink);
		showStatus("");
	} else { showMessage ("No internet connection to looks for beta version.");}
	return macrotextnih;
}

function InstallPluginsStarter(macroname) {
	// from MacroPluginShortcutsTool.txt
	codestarter = "run\(\"Install...\", \"install=[\"+getDirectory(\"macros\")+\""+kind+ macroname + "\]\"\);";
	if (getDirectory("plugins") == "") exit ("Unable to find the Plugins directory; something wrong in the ImageJ folder.");
	if (endsWith(macroname, ".txt") || endsWith(macroname, ".ijm")) pluginname = substring(macroname, 0, (lengthOf(macroname)-4));
	StarterDir = getDirectory("plugins")+favoritefoldername+File.separator;
	File.makeDirectory(StarterDir);
	if (!File.exists(StarterDir)) exit ("Unable to create "+favoritefoldername+" Macros directory, something wrong in the ImageJ folder.");
	starterplugin = StarterDir + pluginname +"_ .ijm";
	f= File.open(StarterDir + pluginname +"_ .ijm");
	print (f,codestarter);
	File.close(f);
	showMessage ("The plugin shortcut \"" +pluginname+ "\" will be available after\nImageJ restarting, in the \"Plugins->" + favoritefoldername + "\" menu.");
}

// *** End of additionnal code for on line update ressources ***

